package com.quasar.hpatient.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.widget.ImageView;

import com.baidu.mobstat.StatService;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.InspactionBean;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.medicalmodel.BaseInfoVersionData;
import com.quasar.hpatient.network.RetrofitConnection;
import com.quasar.hpatient.onInterface.AnotherResultListener;
import com.quasar.hpatient.onInterface.OnInfoVersionListener;
import com.quasar.hpatient.presenter.BaseInfoVersionPresenter;
import com.quasar.hpatient.presenter.TestRecordPresenter;
import com.quasar.hpatient.utils.FileUtil;
import com.quasar.hpatient.utils.FileUtils;
import com.quasar.hpatient.utils.SharedPreferencesUtil;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;


/**
 * author : 许建宏
 * Date: 2017/9/26 0026
 * View:启动页
 */
@EActivity(R.layout.activity_splash)
public class SplashActivity extends BaseActivity implements AnotherResultListener<List<InspactionBean>>{

    @ViewById(R.id.iv_entry)
    ImageView iv_entry;

    private static final int ANIM_TIME = 0;//动画几秒
    private static final float SCALE_END = 1F;//1.15F
    private static final String TAG = "SplashActivity";

    @AfterViews
    void init() {
        ViseLog.d("enter");

        //判断是否为第一次登陆
        boolean isFirstOpen = SharedPreferencesUtil.getBoolean(this, SharedPreferencesUtil.FIRST_OPEN, true);
        if (isFirstOpen) {
            WelcomeGuideActivity_.intent(this).start();
            finish();
            return;
        }else{

            if(!FileUtil.checkFileExists(path + ConstantManger.DATABDNAME)){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (writeDataBase()) {
                            FileUtils.copyFilesFromAssets(SplashActivity.this, ConstantManger.DATABDNAME, path + ConstantManger.DATABDNAME);
                        }
                    }
                }).start();
            }
        }
        startMainActivity();

        StatService.start(this);

    }

    String path = ConstantManger.DARABASEDBPATH;
    public boolean writeDataBase() {
        File file = new File(path);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return true;
            } else
                return false;
        }
        return true;
    }

    //检测本地数据库是否存在更新
    public void TestingUpdate(String json){

        ViseLog.d("enter");

        RetrofitConnection.getRoutineApi().VersionDataService(json)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseBody responseBody) {
                        ViseLog.d("onNext"+responseBody.toString());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        ViseLog.d("onNext"+e.getMessage());
                    }
                    @Override
                    public void onComplete() {
                        ViseLog.d("onComplete");
                    }
                });
    }

    //启动界面进行延迟
    private void startMainActivity() {
        iv_entry.setImageResource(R.mipmap.ic_startups);
        //主线程延时1秒
        Observable.timer(1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
//                        startAnim();
                        LoginActivity_.intent(SplashActivity.this).start();
                        ViseLog.d("enter");
                        finish();
                    }
                });

    }

    //添加界面动画
    private void startAnim() {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(iv_entry, "scaleX", 1f, SCALE_END);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(iv_entry, "scaleY", 1f, SCALE_END);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(ANIM_TIME).play(animatorX).with(animatorY);
        set.start();

        set.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                //MainActivity_.intent(SplashActivity.this).start();
                LoginActivity_.intent(SplashActivity.this).start();
                ViseLog.d("enter");
                finish();
            }
        });

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(List<InspactionBean> data) {
        if(data.size()>0){
            MainAppliaction.getInstance().setInspactionBeanListDB(data);
            LoginActivity_.intent(SplashActivity.this).start();
            ViseLog.d("enter");
            finish();
        }
    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public void onRefreshUI() {

    }
}
